package com.health.mapp.impl;

import org.apache.http.util.TextUtils;
import org.apache.log4j.Logger;

import com.health.db.dto.StuffDetailDto;
import com.health.db.service.user.IStuffListService;
import com.health.mapp.bean.GetStuffOpenIdReq;
import com.health.mapp.bean.GetStuffOpenIdResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;
import com.wxtools.api.IService;
import com.wxtools.bean.result.WxOAuth2AccessTokenResult;

/**
 * Created by yangyayun on 2018/1/11.
 */
public class GetStuffOpenIdImpl implements ActionInterface{

    private IService mpService;
    private IStuffListService stuffListService;

    private static Logger logger = Logger.getLogger(GetStuffOpenIdImpl.class);

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {


        GetStuffOpenIdReq req = (GetStuffOpenIdReq) reqBean;
        GetStuffOpenIdResp _resp = new GetStuffOpenIdResp();

        if (TextUtils.isEmpty(req.getCode()))
        {
            _resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
            _resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);

            return _resp;
        }

        try {
            WxOAuth2AccessTokenResult tokenResult=mpService.oauth2ToGetAccessToken(req.getCode());
            StuffDetailDto stuffDetailDto=stuffListService.selectByOpenId(tokenResult.getOpenid());
            if (stuffDetailDto!=null)
            {
                stuffDetailDto.setPassword(null);
            }else
            {
                _resp.setTokenResult(tokenResult);
            }


        }catch (Exception e)
        {
            logger.error(e.getMessage());
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("code error");
            logger.info("code error");
        }

        return _resp;
    }

    public IService getMpService() {
        return mpService;
    }

    public void setMpService(IService mpService) {
        this.mpService = mpService;
    }

    public IStuffListService getStuffListService() {
        return stuffListService;
    }

    public void setStuffListService(IStuffListService stuffListService) {
        this.stuffListService = stuffListService;
    }
}
